package com.hq.zb.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hq.zb.dto.DeptDto;
import com.hq.zb.dto.UserDto;
import com.hq.zb.entity.CurrentUser;
import com.hq.zb.model.User;
import com.hq.zb.model.UserMenu;
import com.hq.zb.service.IUserMenuService;
import com.hq.zb.service.IUserService;
import com.hq.zb.utils.ContextHolderUtils;
import com.hq.zb.utils.Result;
import com.hq.zb.vo.SelectCommVo;
import com.hq.zb.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理前端控制器
 * @author huanghanrui
 * @date 2022-03-19
 */
@RestController
@RequestMapping("/sys")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserMenuService userMenuService;

    /**
     * 分页查询用户
     * @param userDto 用户
     * @param pageNum 页签
     * @param pageSize 页大小
     * @return
     */
    @PostMapping("/user/query")
    @ResponseBody
    public Result queryUser(@RequestBody UserDto userDto, int pageNum, int pageSize) {
        Page<UserVo> page = new Page<>(pageNum, pageSize);
        Wrapper<UserVo> wrapper = new EntityWrapper<>();
        wrapper.eq("t.delete_flag", false);
        if (StringUtils.isNotEmpty(userDto.getUserName())) {
            wrapper.like("t.user_name", userDto.getUserName());
        }
        if (StringUtils.isNotEmpty(userDto.getName())) {
            wrapper.like("t.name", userDto.getName());
        }
        if (StringUtils.isNotEmpty(userDto.getOrgCode())) {
            wrapper.eq("t.org_code", userDto.getOrgCode());
        }
        if (StringUtils.isNotEmpty(userDto.getDeptCode())) {
            wrapper.eq("t.dept_code", userDto.getDeptCode());
        }
        wrapper.eq("t.sys_source", userDto.getSysSource());
        //过滤超级管理员的
        wrapper.notIn("t.user_name", "admin");
        Page<UserVo> list = userService.listByPage(page, wrapper);
        return Result.data(list);
    }

    /**
     * 新增编辑用户
     * @param userDto
     * @return
     */
    @PostMapping("/user/save")
    public Result saveUser(@RequestBody UserDto userDto) {
        userService.saveUser(userDto);
        return Result.ok();
    }

    /**
     * 删除用户
     * @param users
     * @return
     */
    @PostMapping("/user/delete")
    public Result deleteUser(@RequestBody List<User> users) {
        userService.deleteUser(users);
        return Result.ok();
    }

    /**
     * 分配用户菜单
     * @param UserMenu
     * @return
     */
    @PostMapping("/user/menu/assign")
    public Result assignUserMenu(@RequestBody UserMenu UserMenu) {
        userMenuService.assignUserMenu(UserMenu);
        return Result.ok();
    }

    /**
     * 查询角色菜单
     * @return
     */
    @PostMapping("/user/menu/query")
    @ResponseBody
    public Result queryMenuIds(@RequestBody UserMenu userMenu) {
        return Result.data(userMenuService.queryMenuId(userMenu));
    }

    /**
     * 查询
     * @param userDto 参数
     * @return
     */
    @PostMapping("/user/queryList")
    @ResponseBody
    public Result queryUser(@RequestBody UserDto userDto) {
        //获取当前登陆用户信息
        CurrentUser currentUser = ContextHolderUtils.getCurrentUser();
        List<UserDto> deptVos = userService.queryList(userDto, currentUser);
        String data = JSONObject.toJSONString(deptVos, SerializerFeature.NotWriteDefaultValue);
        return Result.data(JSONObject.parse(data));
    }
}
